package com.hntest.controller;

import com.hntest.entity.CompanyBankInfo;
import com.hntest.entity.Pager;
import com.hntest.service.CompanyBankInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;

@Controller("companyBankInfoController")
public class CompanyBankInfoController {

    private CompanyBankInfoService companyBankInfoService;

    public void setCompanyBankInfoService(CompanyBankInfoService companyBankInfoService) {
        this.companyBankInfoService = companyBankInfoService;
    }

    @RequestMapping(value = "/companyBankInfoSelect", method = {RequestMethod.GET, RequestMethod.POST})
    public String select(Pager pager, CompanyBankInfo companyBankInfo, ModelMap modelMap){
        List<CompanyBankInfo> list=companyBankInfoService.getListByPage(pager, companyBankInfo);
        modelMap.put("list", list);
        modelMap.put("pager",pager);
        modelMap.put("companyBankInfo",companyBankInfo);
        modelMap.put("bankAll", companyBankInfoService.getAllBankName());
        return "companyBankInfoSelect";
    }

    @RequestMapping("/toInsertCompanyBankInfo")//去往新增页面(得到年级下拉框的值)
    public String toInsert(ModelMap modelMap){
        modelMap.put("bankAll", companyBankInfoService.getAllBankName());
        return "companyBankInfoInsert";
    }

    @RequestMapping("/doInsertCompanyBankInfo")//提交新增数据，成功后重定向到分页查询
    public String doInsert(CompanyBankInfo companyBankInfo){
        int n=companyBankInfoService.insert(companyBankInfo);
        if(n>0)
            return "redirect:/companyBankInfoSelect?op=insertYes";
        else
            return "redirect:/companyBankInfoSelect?op=insertNo";
    }

    @RequestMapping("/toUpdateCompanyBankInfo/{cbiid}")//根据id查询出原始银行信息，然后把信息转发到修改页面
    public String toUpdate(@PathVariable(value="cbiid") int cbiid, ModelMap modelMap){
        CompanyBankInfo companyBankInfo=companyBankInfoService.getCompanyBankInfoById(cbiid);
        modelMap.put("old", companyBankInfo);//
        modelMap.put("bankAll", companyBankInfoService.getAllBankName());//银行下拉框
        return "companyBankInfoUpdate";
    }

    @RequestMapping("/doUpdateCompanyBankInfo")//提交更新后的数据，成功后重定向到分页查询
    public String doUpdate(CompanyBankInfo companyBankInfo){
        int n=companyBankInfoService.update(companyBankInfo);
        if(n>0)
            return "redirect:/companyBankInfoSelect?op=updateYes";
        else
            return "redirect:/companyBankInfoSelect?op=updateNo";
    }

}
